﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Threading;

namespace NJL.Web
{
    public partial class RegisterEnterprise : BasePage
    {
        private readonly BLL.NJL_CS_COMPANY_TYPE bllCompanyType = new BLL.NJL_CS_COMPANY_TYPE();
        BLL.NJL_CS_SERVICE_TYPE bllServiceType = new BLL.NJL_CS_SERVICE_TYPE();
        BLL.NJL_USER bllUser = new BLL.NJL_USER();
        BLL.NJL_COMPANY bllCompany = new BLL.NJL_COMPANY();
        Model.NJL_COMPANY modelCompany = new Model.NJL_COMPANY();
        NJL.BLL.NJL_CS_AREA bllArea = new BLL.NJL_CS_AREA();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataSet dsCompanyType = bllCompanyType.GetAllList();
                rblType.DataSource = dsCompanyType.Tables[0];
                rblType.DataTextField = "TYPE_NAME";
                rblType.DataValueField = "TYPE_CODE";
                rblType.DataBind();
                Image3.Visible = false;
                Image4.Visible = false;
                string ProvinceId = WEBSITE_CODE.Substring(0, 2);
                DataSet dscity = bllArea.GetList("AREA_CODE like '" + ProvinceId + "%00' and deep=2");
                ddlArea.DataSource = dscity;
                ddlArea.DataTextField = "AREA_NAME";
                ddlArea.DataValueField = "AREA_CODE";
                ddlArea.DataBind();
                ddlArea.Items.Insert(0, new ListItem("请选择", "0"));
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (bllCompany.isRegister(UserId).Tables[0].Rows.Count > 0)
            {
                Royalsoft.Common.MessageBox.Show(this, "该账户已注册过，请重新注册新的账户");
                return;
            }
            modelCompany.ID = DateTime.Now.ToString("yyyyMMddHHmmssfff") + new Random().Next(100, 999).ToString();
            modelCompany.NJL_SYS_BASE_ID = WebSiteCode;
            modelCompany.COMPANY_NAME = txtName.Text.Trim();
            modelCompany.TYPE_CODE = rblType.SelectedValue;
            modelCompany.ADDRESS = txtAddress.Text;
            modelCompany.AREA_CODE = ddlArea.SelectedValue;
            modelCompany.CONTACT = txtLinkman.Text.Trim();
            modelCompany.TEL = txtTel.Text.Trim();
            modelCompany.MOBILE = txtCellPhone.Text.Trim();
            modelCompany.MSN = txtMsn.Text.Trim();
            modelCompany.QQ = txtQQ.Text.Trim();
            modelCompany.CREATE_DATE_TIME = DateTime.Now;
            modelCompany.NJL_USER_ID = UserId;
            modelCompany.CREATE_USER_ID = UserId;
            modelCompany.CREATE_USER_NAME = UserName;
            modelCompany.AUDIT_STATE = 0;
            modelCompany.AUDIT_INFO_1 = "0";
            modelCompany.AUDIT_INFO_2 = "0";
            modelCompany.AUDIT_INFO_3 = "0";
            modelCompany.AUDIT_INFO_4 = "0";
            modelCompany.CLAIM_STATE = 0;
            modelCompany.COMMENT_COUNT = 0;
            modelCompany.VISIT_COUNT = 0;
            modelCompany.IS_OPEN_BOOK = 0;
            modelCompany.IS_SHOW_CONTACT = 0;
            if (FileUpload1.HasFile)
            {
                if (FileUpload1.PostedFile.ContentType.Contains("image"))
                {
                    if (FileUpload1.PostedFile.ContentLength < 5242880)
                    {
                        modelCompany.AUDIT_INFO_1 = base.SaveUploadFile(Request.Files, 0);
                        Thread.Sleep(1);
                    }
                    else
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传文件大小不能超过5M哦");
                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "只能上传.jpg、.gif格式的图片文件哦");
                }
            }
            if (FileUpload2.HasFile)
            {
                if (FileUpload2.PostedFile.ContentType.Contains("image"))
                {
                    if (FileUpload2.PostedFile.ContentLength < 5242880)
                    {
                        modelCompany.AUDIT_INFO_2 = base.SaveUploadFile(Request.Files, 1);
                        Thread.Sleep(1);
                    }
                    else
                    {
                        Royalsoft.Common.MessageBox.Show(this, "上传文件大小不能超过5M哦");
                    }
                }
                else
                {
                    Royalsoft.Common.MessageBox.Show(this, "只能上传.jpg、.gif格式的图片文件哦");
                }

            }
            if (FileUpload3.HasFile)
            {
                modelCompany.AUDIT_INFO_3 = base.SaveUploadFile(Request.Files, 2);
                Image3.ImageUrl = GetImageUrl(modelCompany.AUDIT_INFO_3);
                Image3.Visible = true;
                Thread.Sleep(1);
            }
            if (FileUpload4.HasFile)
            {
                modelCompany.AUDIT_INFO_4 = base.SaveUploadFile(Request.Files, 3);
                Image4.ImageUrl = GetImageUrl(modelCompany.AUDIT_INFO_4);
                Image4.Visible = true;
                Thread.Sleep(1);
            }
            if (bllCompany.Add(modelCompany))
            {
                Royalsoft.Common.MessageBox.ShowAndRedirect(this, "您的资料已成功提交,请等待管理员进行审核", "/AdminVisitor/Default.aspx");
                //modelUser = bllUser.GetModel(UserId);
                //if (modelUser != null)
                //{
                //    modelUser.TYPE_CODE = "3";
                //    //更新用户类型
                //    if (bllUser.Update(modelUser))
                //    {
                //        Session.Add("usertype", "3");

                //    }
                //}
            }

        }


    }
}